/*     */ package com.bj58.analysis.tools.common;
/*     */ 
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLDecoder;
/*     */ import java.net.URLEncoder;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class StringHelper
/*     */ {
/*     */   public static String urlDecode(String content, String charset)
/*     */     throws UnsupportedEncodingException
/*     */   {
/*  19 */     if (content != null) {
/*  20 */       return URLDecoder.decode(content, charset);
/*     */     }
/*  22 */     return "";
/*     */   }
/*     */   
/*     */   public static String urlEncode(String content, String charset) throws UnsupportedEncodingException
/*     */   {
/*  27 */     return URLEncoder.encode(content, charset);
/*     */   }
/*     */   
/*     */   public static String encode(String source, String from, String to) throws UnsupportedEncodingException
/*     */   {
/*  32 */     byte[] bts = source.getBytes(from);
/*  33 */     return new String(bts, to);
/*     */   }
/*     */   
/*     */   public static String parseCharacterEncoding(String contentType) {
/*  37 */     if (contentType == null)
/*  38 */       return null;
/*  39 */     int start = contentType.indexOf("charset=");
/*  40 */     if (start < 0)
/*  41 */       return null;
/*  42 */     String encoding = contentType.substring(start + 8);
/*  43 */     int end = encoding.indexOf(';');
/*  44 */     if (end >= 0)
/*  45 */       encoding = encoding.substring(0, end);
/*  46 */     encoding = encoding.trim();
/*  47 */     if ((encoding.length() > 2) && (encoding.startsWith("\"")) && (encoding.endsWith("\"")))
/*  48 */       encoding = encoding.substring(1, encoding.length() - 1);
/*  49 */     return encoding.trim();
/*     */   }
/*     */   
/*     */   public static boolean isEmpty(String str)
/*     */   {
/*  54 */     return (str == null) || (str.equals(""));
/*     */   }
/*     */   
/*  57 */   private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/*     */   
/*     */   public static String toHexString(byte[] buf)
/*     */   {
/*  61 */     return toHexString(buf, null, Integer.MAX_VALUE);
/*     */   }
/*     */   
/*     */   public static String toHexString(byte[] buf, String sep, int lineLen) {
/*  65 */     if (buf == null)
/*  66 */       return null;
/*  67 */     if (lineLen <= 0) {
/*  68 */       lineLen = Integer.MAX_VALUE;
/*     */     }
/*  70 */     StringBuilder res = new StringBuilder(buf.length * 2);
/*  71 */     for (int i = 0; i < buf.length; i++) {
/*  72 */       int b = buf[i];
/*  73 */       res.append(HEX_DIGITS[(b >> 4 & 0xF)]);
/*  74 */       res.append(HEX_DIGITS[(b & 0xF)]);
/*  75 */       if ((i > 0) && (i % lineLen == 0)) {
/*  76 */         res.append('\n');
/*  77 */       } else if ((sep != null) && (i < lineLen - 1))
/*  78 */         res.append(sep);
/*     */     }
/*  80 */     return res.toString();
/*     */   }
/*     */   
/*     */   private static final int charToNibble(char c) {
/*  84 */     if ((c >= '0') && (c <= '9'))
/*  85 */       return c - '0';
/*  86 */     if ((c >= 'a') && (c <= 'f'))
/*  87 */       return 10 + (c - 'a');
/*  88 */     if ((c >= 'A') && (c <= 'F')) {
/*  89 */       return 10 + (c - 'A');
/*     */     }
/*  91 */     return -1;
/*     */   }
/*     */   
/*     */   public static byte[] fromHexString(String text)
/*     */   {
/*  96 */     text = text.trim();
/*  97 */     if (text.length() % 2 != 0)
/*  98 */       text = "0" + text;
/*  99 */     int resLen = text.length() / 2;
/*     */     
/* 101 */     byte[] res = new byte[resLen];
/* 102 */     for (int i = 0; i < resLen; i++) {
/* 103 */       int j = i << 1;
/* 104 */       int hiNibble = charToNibble(text.charAt(j));
/* 105 */       int loNibble = charToNibble(text.charAt(j + 1));
/* 106 */       if ((loNibble == -1) || (hiNibble == -1))
/* 107 */         return null;
/* 108 */       res[i] = ((byte)(hiNibble << 4 | loNibble));
/*     */     }
/* 110 */     return res;
/*     */   }
/*     */   
/*     */   public static String join(String[] arr, String split) {
/* 114 */     if ((arr != null) && (arr.length > 0)) {
/* 115 */       StringBuilder stringBuilder = new StringBuilder();
/* 116 */       for (String s : arr) {
/* 117 */         stringBuilder.append(s).append(split);
/*     */       }
/* 119 */       stringBuilder.delete(stringBuilder.length() - split.length(), stringBuilder.length());
/* 120 */       return stringBuilder.toString();
/*     */     }
/* 122 */     return "";
/*     */   }
/*     */   
/*     */   public static String join(Object[] arr, String split)
/*     */   {
/* 127 */     if ((arr != null) && (arr.length > 0)) {
/* 128 */       StringBuilder stringBuilder = new StringBuilder();
/* 129 */       for (Object s : arr) {
/* 130 */         stringBuilder.append(String.valueOf(s)).append(split);
/*     */       }
/* 132 */       stringBuilder.delete(stringBuilder.length() - split.length(), stringBuilder.length());
/* 133 */       return stringBuilder.toString();
/*     */     }
/* 135 */     return "";
/*     */   }
/*     */   
/*     */   public static Properties parseProperties(String data, String split)
/*     */   {
/* 140 */     Properties properties = new Properties();
/* 141 */     if ((!isEmpty(split)) && (!isEmpty(data))) {
/* 142 */       for (String dd : data.split(split)) {
/* 143 */         String[] d = dd.split("=");
/* 144 */         if (d.length == 2) {
/* 145 */           properties.put(d[0], d[1]);
/*     */         }
/*     */       }
/*     */     }
/* 149 */     return properties;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static String splitAndFilterString(String input)
/*     */   {
/* 156 */     if ((input == null) || (input.trim().equals(""))) {
/* 157 */       return "";
/*     */     }
/*     */     
/* 160 */     String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "");
/* 161 */     str = str.replaceAll("[(/>)<]", "");
/* 162 */     return str;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static String fromMapToHiveString(Map<String, String> fromMap, String CollItemDelim, String MapKeyDelim)
/*     */   {
/* 169 */     StringBuffer result = new StringBuffer("");
/*     */     
/* 171 */     for (String s : fromMap.keySet())
/*     */     {
/*     */ 
/* 174 */       if (!"".equals(result.toString()))
/*     */       {
/* 176 */         result.append(CollItemDelim);
/*     */       }
/* 178 */       result.append(s);
/* 179 */       result.append(MapKeyDelim);
/* 180 */       result.append((String)fromMap.get(s));
/*     */     }
/*     */     
/*     */ 
/* 184 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static String fromMapToHiveString(Map<String, String> fromMap)
/*     */   {
/* 189 */     StringBuffer result = new StringBuffer("");
/*     */     
/* 191 */     for (String s : fromMap.keySet())
/*     */     {
/*     */ 
/* 194 */       if (!"".equals(result.toString()))
/*     */       {
/* 196 */         result.append("\002");
/*     */       }
/* 198 */       result.append(s);
/* 199 */       result.append("\003");
/* 200 */       result.append((String)fromMap.get(s));
/*     */     }
/*     */     
/*     */ 
/* 204 */     return result.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Map<String, String> fromStringToMap(String line, String CollItemDelim, String MapKeyDelim)
/*     */   {
/* 212 */     Map<String, String> result = new HashMap();
/*     */     
/*     */ 
/* 215 */     line = line.replace("，", ",").replace("｛", "{").replace("｝", "}").replace("：", ":").replace("‘", "'").replace("’", "'");
/*     */     
/*     */ 
/* 218 */     if ((line.startsWith("{")) && (line.endsWith("}")))
/*     */     {
/* 220 */       line = line.substring(1);
/* 221 */       line = line.substring(0, line.length() - 1);
/*     */     }
/*     */     
/* 224 */     int i = 0;
/* 225 */     while (i > -1) {
/* 226 */       i = line.indexOf(CollItemDelim);
/* 227 */       if (i > -1)
/*     */       {
/*     */ 
/*     */         try
/*     */         {
/*     */ 
/* 233 */           String tmp = line.substring(line.indexOf(MapKeyDelim) + 1, i);
/* 234 */           if (tmp.startsWith("'"))
/* 235 */             while (!tmp.endsWith("'")) {
/* 236 */               i = line.indexOf(CollItemDelim, i + 1);
/*     */               
/* 238 */               tmp = line.substring(0, i);
/*     */             }
/* 240 */           if (tmp.startsWith("{")) {
/* 241 */             while (!tmp.endsWith("}")) {
/* 242 */               i = line.indexOf(CollItemDelim, i + 1);
/* 243 */               tmp = line.substring(0, i);
/*     */             }
/*     */           }
/*     */         }
/*     */         catch (Exception e) {}
/*     */       }
/* 249 */       String firstpart = "";
/*     */       try {
/* 251 */         firstpart = line.substring(0, i);
/* 252 */         line = line.substring(i + 1);
/*     */       }
/*     */       catch (Exception e) {
/* 255 */         firstpart = line;
/*     */       }
/* 257 */       String key = "";
/* 258 */       String value = "";
/* 259 */       String kv = firstpart;
/*     */       try
/*     */       {
/* 262 */         if ((kv.split(MapKeyDelim)[0].startsWith("'")) && (kv.split(MapKeyDelim)[0].endsWith("'"))) {
/* 263 */           key = kv.split(MapKeyDelim)[0].split("'")[1].trim();
/*     */           
/* 265 */           if (!kv.endsWith(MapKeyDelim))
/*     */           {
/* 267 */             value = kv.split(MapKeyDelim, 2)[1];
/* 268 */             value = value.substring(1);
/* 269 */             value = value.substring(0, value.length() - 1);
/*     */           }
/*     */         }
/*     */         else {
/* 273 */           key = kv.split(MapKeyDelim)[0].trim();
/*     */           
/* 275 */           if (!kv.endsWith(MapKeyDelim)) {
/* 276 */             value = kv.split(MapKeyDelim, 2)[1].trim();
/*     */           }
/*     */         }
/*     */       } catch (Exception e) {
/*     */         try {
/* 281 */           key = kv.split(MapKeyDelim)[0].trim();
/* 282 */           value = kv.split(MapKeyDelim, 2)[1].trim();
/*     */         }
/*     */         catch (Exception e1) {}
/*     */       }
/*     */       
/* 287 */       result.put(key, value);
/*     */     }
/* 289 */     return result;
/*     */   }
/*     */ }


/* Location:              C:\Users\zhaoxiang\Desktop\basestat_log_common_tools-0.3.jar!\com\bj58\analysis\tools\common\StringHelper.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       0.7.1
 */